﻿Imports System.Collections.ObjectModel
Imports System.Xml.Serialization
Imports System.IO.IsolatedStorage
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Reflection
Imports System.Text

Namespace My

    Namespace Computer

        Public NotInheritable Class FileSystem

            Public Shared Function ReadAllText(ByVal fn As String) As String
                Dim txt As String = ""
                Dim fileStorage = IsolatedStorageFile.GetUserStoreForApplication()
                Using isolatedFileStream = New IsolatedStorageFileStream(fn, FileMode.Open, fileStorage)
                    Dim txtReader As TextReader = New StreamReader(isolatedFileStream, True)
                    txt = txtReader.ReadToEnd()
                    isolatedFileStream.Close()
                End Using
                Return txt
            End Function

            Public Shared Function ReadAllText(ByVal s As IO.Stream) As String
                Dim txtReader As TextReader = New StreamReader(s, True)
                Return txtReader.ReadToEnd()
            End Function

            Public Shared Function ReadAllText(ByVal fn As String, ByVal en As Encoding) As String
                Dim txt As String = ""
                Dim fileStorage = IsolatedStorageFile.GetUserStoreForApplication()
                Using isolatedFileStream = New IsolatedStorageFileStream(fn, FileMode.Open, fileStorage)
                    Dim txtReader As TextReader = New StreamReader(isolatedFileStream, en)
                    txt = txtReader.ReadToEnd()
                    isolatedFileStream.Close()
                End Using
                Return txt
            End Function

            Public Shared Function ReadAllText(ByVal s As IO.Stream, ByVal en As Encoding) As String
                Dim txtReader As TextReader = New StreamReader(s, en)
                Return txtReader.ReadToEnd()
            End Function

        End Class

    End Namespace

    Namespace Application

        Public NotInheritable Class Info

            Private Shared CurrentAssembly As Assembly = System.Reflection.Assembly.GetCallingAssembly()
            Private Shared m_CompanyName As String = ""
            Private Shared m_Copyright As String = ""
            Private Shared m_Description As String = ""
            Private Shared m_ProductName As String = ""
            Private Shared m_Trademark As String = ""

            Private Shared Function GetAttribute(ByVal AttributeType As Type) As Object
                Dim customAttributes As Object() = CurrentAssembly.GetCustomAttributes(AttributeType, True)
                If (customAttributes.Length = 0) Then
                    Return Nothing
                End If
                Return customAttributes(0)
            End Function

            Public Shared ReadOnly Property AssemblyName() As String
                Get
                    Return CurrentAssembly.GetName(True).Name
                End Get
            End Property

            Public Shared ReadOnly Property CompanyName As String
                Get
                    If (Info.m_CompanyName Is Nothing) Then
                        Dim attribute As AssemblyCompanyAttribute = DirectCast(Info.GetAttribute(GetType(AssemblyCompanyAttribute)), AssemblyCompanyAttribute)
                        If (attribute Is Nothing) Then
                            Info.m_CompanyName = ""
                        Else
                            Info.m_CompanyName = attribute.Company
                        End If
                    End If
                    Return Info.m_CompanyName
                End Get
            End Property

            Public Shared ReadOnly Property Copyright As String
                Get
                    If (Info.m_Copyright Is Nothing) Then
                        Dim attribute As AssemblyCopyrightAttribute = DirectCast(Info.GetAttribute(GetType(AssemblyCopyrightAttribute)), AssemblyCopyrightAttribute)
                        If (attribute Is Nothing) Then
                            Info.m_Copyright = ""
                        Else
                            Info.m_Copyright = attribute.Copyright
                        End If
                    End If
                    Return Info.m_Copyright
                End Get
            End Property

            Public Shared ReadOnly Property Description As String
                Get
                    If (Info.m_Description Is Nothing) Then
                        Dim attribute As AssemblyDescriptionAttribute = DirectCast(Info.GetAttribute(GetType(AssemblyDescriptionAttribute)), AssemblyDescriptionAttribute)
                        If (attribute Is Nothing) Then
                            Info.m_Description = ""
                        Else
                            Info.m_Description = attribute.Description
                        End If
                    End If
                    Return Info.m_Description
                End Get
            End Property

            Public Shared ReadOnly Property ProductName As String
                Get
                    If (Info.m_ProductName Is Nothing) Then
                        Dim attribute As AssemblyProductAttribute = DirectCast(Info.GetAttribute(GetType(AssemblyProductAttribute)), AssemblyProductAttribute)
                        If (attribute Is Nothing) Then
                            Info.m_ProductName = ""
                        Else
                            Info.m_ProductName = attribute.Product
                        End If
                    End If
                    Return Info.m_ProductName
                End Get
            End Property

            Public Shared ReadOnly Property Trademark As String
                Get
                    If (Info.m_Trademark Is Nothing) Then
                        Dim attribute As AssemblyTrademarkAttribute = DirectCast(Info.GetAttribute(GetType(AssemblyTrademarkAttribute)), AssemblyTrademarkAttribute)
                        If (attribute Is Nothing) Then
                            Info.m_Trademark = ""
                        Else
                            Info.m_Trademark = attribute.Trademark
                        End If
                    End If
                    Return Info.m_Trademark
                End Get
            End Property

            Public Shared ReadOnly Property Title() As String
                Get
                    Return CurrentAssembly.GetName(True).FullName
                End Get
            End Property

            Public Shared ReadOnly Property Version() As Version
                Get
                    Dim ver As String = CurrentAssembly.ToString()
                    ver = Helper.GetRight(ver, "Version=").Trim()
                    ver = Helper.GetLeft(ver, ",").Trim()
                    Return New Version(ver)
                End Get
            End Property

        End Class

        Namespace Settings

            <XmlRoot("configuration")> _
            Public Class Configuration
                Private Shared m_current As Configuration

                Public Shared Sub Save()
                    If (Not Configuration.Current Is Nothing) Then
                        Using file As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
                            Dim path As String = "Configuration.xml"
                            Using stream As IsolatedStorageFileStream = New IsolatedStorageFileStream(path, FileMode.Create, file)
                                Dim xs As New XmlSerializer(GetType(Configuration))
                                xs.Serialize(stream, Configuration.Current)
                            End Using
                        End Using
                    End If
                End Sub

                Public Shared ReadOnly Property Current As Configuration
                    Get
                        If (Configuration.m_current Is Nothing) Then
                            Using file As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
                                Dim path As String = "Configuration.xml"
                                If Not file.FileExists(path) Then
                                    Configuration.m_current = New Configuration
                                Else
                                    Try
                                        Using stream As IsolatedStorageFileStream = New IsolatedStorageFileStream(path, FileMode.Open, file)
                                            Dim serializer As New XmlSerializer(GetType(Configuration))
                                            Configuration.m_current = TryCast(serializer.Deserialize(stream), Configuration)
                                        End Using
                                    Catch ex As Exception
                                        Configuration.m_current = New Configuration
                                    End Try
                                End If
                            End Using
                        End If
                        Return Configuration.m_current
                    End Get
                End Property

            End Class

        End Namespace

    End Namespace

End Namespace